package cn.com.api.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_user_friend_group_detail;

import cn.com.api.mapper.ApiU_user_friend_group_detailMapper;

import cn.com.api.service.ApiU_user_friend_group_detailService;
import cn.com.api.service.impl.base.U_user_friend_group_detailServiceImplBase;
import cn.com.util.PageInfo;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class ApiU_user_friend_group_detailServiceImpl extends U_user_friend_group_detailServiceImplBase
		implements ApiU_user_friend_group_detailService {
	@Autowired
	private ApiU_user_friend_group_detailMapper apiU_user_friend_group_detailMapper;

	// 新增对象后返回记录ID
	public int U_user_friend_group_detailInsertion(ApiU_user_friend_group_detail u_user_friend_group_detail) {
		return this.apiU_user_friend_group_detailMapper.U_user_friend_group_detailInsertion(u_user_friend_group_detail);
	}

	public List<ApiU_user_friend_group_detail> getfriend_group_details(Map map, PageInfo page) {
		return this.apiU_user_friend_group_detailMapper.findU_user_friend_group_detailsByMap(map, page);
	}

	public boolean checkUserExistGroup(String user_ids, String group_id) {
		boolean ret = true;
		List<ApiU_user_friend_group_detail> group_detail_list = this.apiU_user_friend_group_detailMapper
				.findU_user_friend_group_detailsByGroupID(group_id);
		String f_ids[] = user_ids.split(",");
		for (String f_id : f_ids) {
			for (ApiU_user_friend_group_detail group_detail : group_detail_list) {
				if (f_id.equals(group_detail.getFriend_id())) {
					return false;
				}
			}
		}
		return ret;
	}

	// 从一个分组移到另一个分组
	public void addMember2Group(String old_group_id, String new_group_id, String friend_ids, String type) {
		String[] f_ids = friend_ids.split(",");
		for (String f_id : f_ids) {
			ApiU_user_friend_group_detail detail = this.apiU_user_friend_group_detailMapper
					.findU_user_friend_group_detailsByGroupIDUserID(old_group_id, f_id, Integer.valueOf(type));
			detail.setAdd_time(Util.CurrentTime());
			detail.setFriend_group_id(new_group_id);
			this.apiU_user_friend_group_detailMapper.updateU_user_friend_group_detail(detail);
		}
	}

	public ApiU_user_friend_group_detail getFriendGroupDetail(String group_id, String friend_id) {
		return this.apiU_user_friend_group_detailMapper.findU_user_friend_group_detailsByGroupIDUserID(group_id,
				friend_id, 0);
	}
}
